#include "tft_lcd.h"
#include "string.h"
#include "font.h"
struct _s_lcd lcd;
//画线
//x1,y1:起点坐标
//x2,y2:终点坐标  
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint16_t color)
{
	
	uint16_t t; 
	int xerr=0,yerr=0,delta_x,delta_y,distance; 
	int incx,incy,uRow,uCol; 
	delta_x=x2-x1; //计算坐标增量 
	delta_y=y2-y1; 
	uRow=x1; 
	uCol=y1; 
	if(delta_x>0)incx=1; //设置单�?�方�? 
	else if(delta_x==0)incx=0;//垂直�? 
	else {incx=-1;delta_x=-delta_x;} 
	if(delta_y>0)incy=1; 
	else if(delta_y==0)incy=0;//水平�? 
	else{incy=-1;delta_y=-delta_y;} 
	if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标�? 
	else distance=delta_y; 
	for(t=0;t<=distance+1;t++ )//画线输出 
	{  
		lcd.DrawPoint(uRow,uCol,color);//画点 
		xerr+=delta_x ; 
		yerr+=delta_y ; 
		if(xerr>distance) 
		{ 
			xerr-=distance; 
			uRow+=incx; 
		} 
		if(yerr>distance) 
		{ 
			yerr-=distance; 
			uCol+=incy; 
		} 
	}  
}   
//画矩�?	  
//(x1,y1),(x2,y2):矩形的�?��?�坐�?
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint16_t color)
{
	LCD_DrawLine(x1,y1,x2,y1,color);
	LCD_DrawLine(x1,y1,x1,y2,color);
	LCD_DrawLine(x1,y2,x2,y2,color);
	LCD_DrawLine(x2,y1,x2,y2,color);
}
//在指定位�?画一�?指定大小的圆
//(x,y):�?心点
//r    :半径
void LCD_Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r)
{
	int a,b;
	int di;
	a=0;b=r;	  
	di=3-(r<<1);             //判断下个点位�?的标�?
	while(a<=b)
	{
		lcd.DrawPoint(x0+a,y0-b,lcd.POINT_COLOR);             //5
 		lcd.DrawPoint(x0+b,y0-a,lcd.POINT_COLOR);             //0           
		lcd.DrawPoint(x0+b,y0+a,lcd.POINT_COLOR);             //4               
		lcd.DrawPoint(x0+a,y0+b,lcd.POINT_COLOR);             //6 
		lcd.DrawPoint(x0-a,y0+b,lcd.POINT_COLOR);             //1       
 		lcd.DrawPoint(x0-b,y0+a,lcd.POINT_COLOR);             
		lcd.DrawPoint(x0-a,y0-b,lcd.POINT_COLOR);             //2             
  		lcd.DrawPoint(x0-b,y0-a,lcd.POINT_COLOR);             //7     	         
		a++;
		//使用Bresenham算法画圆    
		if(di<0)di +=4*a+6;	  
		else
		{
			di+=10+4*(a-b);   
			b--;
		} 						    
	}
} 									  
//在指定位�?显示一�?字�??
//x,y:起�?�坐�?
//num:要显示的字�??:" "--->"~"
//size:字体大小 12/16/24
//mode:叠加方式(1)还是非叠加方�?(0)
void LCD_ShowChar(uint16_t x,uint16_t y,uint8_t num,uint8_t size,uint8_t mode)
{  							  
    uint8_t temp,t1,t;
	uint16_t y0=y;
	uint8_t csize=(size/8+((size%8)?1:0))*(size/2);		//得到字体一�?字�?��?�应点阵集所占的字节�?	
 	num=num-' ';//得到偏移后的值（ASCII字库�?从空格开始取模，所�?-' '就是对应字�?�的字库�?
	for(t=0;t<csize;t++)
	{   
		if(size==12)temp=asc2_1206[num][t]; 	 	//调用1206字体
		else if(size==16)temp=asc2_1608[num][t];	//调用1608字体
		else if(size==24)temp=asc2_2412[num][t];	//调用2412字体
		else return;								//没有的字�?
		for(t1=0;t1<8;t1++)
		{			    
			if(temp&0x80)lcd.DrawPoint(x,y,lcd.POINT_COLOR);
			else if(mode==0)lcd.DrawPoint(x,y,lcd.BACK_COLOR);
			temp<<=1;
			y++;
			if(y>=lcd.LCD_VS_CNT)return;		//超区域了
			if((y-y0)==size)
			{
				y=y0;
				x++;
				if(x>=lcd.LCD_HS_CNT)return;	//超区域了
				break;
			}
		}  	 
	}  	    	   	 	  
}   
//m^n函数
//返回�?:m^n次方.
uint32_t LCD_Pow(uint8_t m,uint8_t n)
{
	uint32_t result=1;	 
	while(n--)result*=m;    
	return result;
}			 
//显示数字,高位�?0,则不显示
//x,y :起点坐标	 
//len :数字的位�?
//size:字体大小
//color:颜色 
//num:数�?(0~4294967295);	
void LCD_ShowNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t size)
{         	
	uint8_t t,temp;
	uint8_t enshow=0;						   
	for(t=0;t<len;t++)
	{
		temp=(num/LCD_Pow(10,len-t-1))%10;
		if(enshow==0&&t<(len-1))
		{
			if(temp==0)
			{
				LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
				continue;
			}else enshow=1; 
		 	 
		}
	 	LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0); 
	}
} 
//显示数字,高位�?0,还是显示
//x,y:起点坐标
//num:数�?(0~999999999);	 
//len:长度(即�?�显示的位数)
//size:字体大小
//mode:
//[7]:0,不填�?;1,�?�?0.
//[6:1]:保留
//[0]:0,非叠加显�?;1,叠加显示.
void LCD_ShowxNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t size,uint8_t mode)
{  
	uint8_t t,temp;
	uint8_t enshow=0;						   
	for(t=0;t<len;t++)
	{
		temp=(num/LCD_Pow(10,len-t-1))%10;
		if(enshow==0&&t<(len-1))
		{
			if(temp==0)
			{
				if(mode&0X80)LCD_ShowChar(x+(size/2)*t,y,'0',size,mode&0X01);  
				else LCD_ShowChar(x+(size/2)*t,y,' ',size,mode&0X01);  
 				continue;
			}else enshow=1; 
		 	 
		}
	 	LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode&0X01); 
	}
} 

//显示字�?�串
//x,y:起点坐标
//width,height:区域大小  
//size:字体大小
//*p:字�?�串起�?�地址			  
void LCD_ShowString(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t size,uint8_t *p)
{         
	uint8_t x0=x;
	LCD_Fill(x,y,width,height,lcd.BACK_COLOR);
	width+=x;
	height+=y;
    while((*p<='~')&&(*p>=' '))//判断�?不是非法字�??!
    {       
        if(x>=width){x=x0;y+=size;}
        if(y>=height)break;//退�?
        LCD_ShowChar(x,y,*p,size,0);
        x+=size/2;
        p++;
    }  
}

void LCD_Fill(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint16_t color)
{
		uint16_t i,j;
		for(j = y;j<(y+height);j++)
			for(i = x;i<(x+width);i++)
			{
				lcd.DrawPoint(i,j,color);
			}
}

void LCD_DrawBMP(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint16_t *p)
{
    uint16_t i,j;
		for(j = y;j<(y+height);j++)
			for(i = x;i<(x+width);i++)
			{
				lcd.DrawPoint(i,j,*p++);
			}
		
}


/****************************************************************************
* �?    称：void GUI_HZ(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
* �?    能：
* 入口参数 c显示汉字的编�?
* 出口参数�?
* �?    明：
* 调用方法:
****************************************************************************/

void GUI_HZ16(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
{
  uint8_t i=0;
  uint8_t j=0;
	uint8_t k=0; 
  uint16_t tmp_char=0;
	for(j=0;j<16;j++)
		{
			tmp_char=(Hanzi_16x16[c*32+j*2+1]<<8) + (Hanzi_16x16[c*32+2*j]);//取�?�地址数据
			for(k=0;k<16;k++)
			{
				if((tmp_char&0x01) == 0x01)
				{
					lcd.DrawPoint(x+16*i+k,y+j,Color); // 字�?��?�色
        }else
				{
					lcd.DrawPoint(x+16*i+k,y+j,bkColor); // 背景颜色
        }
				tmp_char=tmp_char >> 1;
     }
  }
}

void GUI_HZ24(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
{
  uint8_t i=0;
  uint8_t j=0;
	uint8_t k=0; 
  uint32_t tmp_char=0;
	for(j=0;j<24;j++)
		{
			tmp_char = (Hanzi_24x24[c*72+3*j]) + (Hanzi_24x24[c*72+j*3+1]<<8) + (Hanzi_24x24[c*72+j*3+2]<<16);//取�?�地址数据
			for(k=0;k<24;k++)
			{
				if((tmp_char&0x01) == 0x01)
				{
					lcd.DrawPoint(x+24*i+k,y+j,Color); // 字�?��?�色
        }else
				{
					lcd.DrawPoint(x+24*i+k,y+j,bkColor); // 背景颜色
        }
				tmp_char=tmp_char >> 1;
     }
  }
}

/****************************************************************************
* �?    称：void GUI_HZ24(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
* �?    能：
* 入口参数 c显示汉字的编�?
* 出口参数�?
* �?    明：
* 调用方法:
****************************************************************************/
void GUI_HZ32(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
{
  uint8_t j=0;
	uint8_t k=0; 
  uint32_t tmp_char=0;
	for(j=0;j<32;j++)
		{
			tmp_char=(Hanzi_32x32[c*128+j*4]) + (Hanzi_32x32[c*128+j*4+1]<<8) + (Hanzi_32x32[c*128+j*4+2]<<16) + (Hanzi_32x32[c*128+j*4+3]<<24);//取�?�地址数据
			
			for(k=0;k<32;k++)
			{
				if((tmp_char&0x01) == 0x01)
				{
					lcd.DrawPoint(x+k,y+j,Color); // 字�?��?�色
        }
				else
				{
					lcd.DrawPoint(x+k,y+j,bkColor); // 背景颜色
        }
				tmp_char=tmp_char >> 1;
     }
  }
}
/****************************************************************************
* �?    称：void GUI_HZ(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
* �?    能：
* 入口参数 c要写的数�?
* 出口参数�?
* �?    明：
* 调用方法:
****************************************************************************/

void GUI_SZ(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
{
  uint8_t j=0;
	uint8_t k=0; 
  uint32_t tmp_char=0;
	for(j=0;j<48;j++)
		{
			tmp_char = 0;
			tmp_char=(Shuzi_16x32[c*144+j*3+2]<<16) + (Shuzi_16x32[c*144+j*3+1]<<8) + Shuzi_16x32[c*144+3*j];//取�?�地址数据
			for(k=0;k<24;k++)
			{
				if((tmp_char&0x01) == 0x01)
				{
					lcd.DrawPoint(x+k,y+j,Color); // 字�?��?�色
        }else
				{
					lcd.DrawPoint(x+k,y+j,bkColor); // 背景颜色
        }
				tmp_char=tmp_char >> 1;
     }
  }
}

/****************************************************************************
* �?    称：void GUI_HZ(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
* �?    能：
* 入口参数 c显示汉字的编�?
* 出口参数�?
* �?    明：
* 调用方法:
****************************************************************************/


//ascii_8x16

void GUI_SZ16(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
{
  uint8_t i=0;
  uint8_t j=0;
	uint8_t k=0; 
  uint16_t tmp_char=0;
	for(j=0;j<16;j++)
		{
			tmp_char=ascii_8x16[c*16+j];//取�?�地址数据(ascii_8x16[c*16+2*j+1])<<8)ascii_8x16[c*16+2*j]
			//tmp_char =tmp_char<< 4;
			for(k=0;k<8;k++)
			{
				if((tmp_char&0x01) == 0x01)
				{
					lcd.DrawPoint(x+8*i+k,y+j,Color); // 字�?��?�色
        }else
				{
					lcd.DrawPoint(x+8*i+k,y+j,bkColor); // 背景颜色
        }
				tmp_char=tmp_char >> 1;
     }
  }
}



void GUI_SZ24(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
{
  uint8_t i=0;
  uint8_t j=0;
	uint8_t k=0; 
  uint16_t tmp_char=0;
	for(j=0;j<24;j++)
		{
			tmp_char=((Shuzi_16x24[c*48+2*j+1])<<8) + (Shuzi_16x24[c*48+2*j]);//取�?�地址数据
			//tmp_char =tmp_char<< 4;
			for(k=0;k<12;k++)
			{
				if((tmp_char&0x01) == 0x01)
				{
					lcd.DrawPoint(x+16*i+k,y+j,Color); // 字�?��?�色
        }else
				{
					lcd.DrawPoint(x+16*i+k,y+j,bkColor); // 背景颜色
        }
				tmp_char=tmp_char >> 1;
     }
  }
}
void GUI_SZ32(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
{
  uint8_t j=0;
	uint8_t k=0; 
  uint32_t tmp_char=0;
	for(j=0;j<32;j++)
		{
			tmp_char=(Shuzi_32x16[c*64+j*2]) + (Shuzi_32x16[c*64+j*2+1]<<8); //+ (Shuzi_32x16[c*64+j*4+2]<<16) + (Shuzi_32x16[c*64+j*4+3]<<24);//??????
			
			for(k=0;k<16;k++)
			{
				if((tmp_char&0x01) == 0x01)
				{
					lcd.DrawPoint(x+k,y+j,Color); // ????
        }
				else
				{
					lcd.DrawPoint(x+k,y+j,bkColor); // ????
        }
				tmp_char=tmp_char >> 1;
     }
  }
}

void GUI_JT(uint16_t x, uint16_t y, uint8_t c,uint16_t Color, uint16_t bkColor)
{
  uint8_t j=0;
	uint8_t k=0; 
  uint32_t tmp_char=0;
	for(j=0;j<40;j++)
		{
			tmp_char = 0;
			tmp_char = (Shuzi_16x32[4*j+3]<<16);
			tmp_char = tmp_char<<8;
			tmp_char=(Shuzi_16x32[j*4]) + (Shuzi_16x32[j*4+1]<<8) + (Shuzi_16x32[j*4+2]<<16);//取�?�地址数据
			for(k=0;k<32;k++)
			{
				if((tmp_char&0x01) == 0x01)
				{
					lcd.DrawPoint(x+k,y+j,Color); // 字�?��?�色
        }else
				{
					lcd.DrawPoint(x+k,y+j,bkColor); // 背景颜色
        }
				tmp_char=tmp_char >> 1;
     }
  }	
}

void GUI_Text(uint16_t x, uint16_t y, uint8_t *str, uint16_t len,uint16_t Color, uint16_t bkColor)
{
  uint8_t i;
  for (i=0;i<len;i++)
  {
    //LED_PutChar((x+8*i),y,*str++,Color,bkColor);
		LCD_ShowChar((x+8*i),y,*str++,16,0);
  }
}
